//
//  EXSpeedTest.m
//  ORM
//
//  Created by PH on 28.12.09.
//  Copyright 2009-2010 Codesign. All rights reserved.
//

#import "EXSpeedTest.h"

@implementation EXPerson

@synthesize firstName;
@synthesize lastName;
@synthesize UID;
@synthesize age;

- (NSString*)description {
	return [NSString stringWithFormat: @"%@ %@ %@ %@", firstName, lastName, UID, age];
}

@end

@implementation EXSpeedTest

- (id)initWithObjectStore:(EXObjectStore*)_objectStore {
	if ((self = [super init])) {
		objectStore = [_objectStore retain];
	}
	return self;
}

- (void)writeTest1 {
	NSMutableArray* objects = [NSMutableArray array];
	for (int i = 0; i < 10000; i++) {
		EXPerson* person = [[EXPerson alloc] init];
		person.firstName = @"First";
		person.lastName = @"Last";
		person.UID = [NSNumber numberWithInt: i];
		person.age = [NSNumber numberWithInt: i % 50];
		[objects addObject: person];
		[person release];
	}
	
	NSDate* pointZero = [NSDate date];
	EXTransaction* transaction = [objectStore beginTransaction];
	@try {
		for (EXPerson* person in objects) {
			[transaction storeObject: person];
		}
		[transaction rollback];
	} @catch (NSException* exception) {
		[transaction rollback];
	}
	NSLog(@"write test: %f secs", -[pointZero timeIntervalSinceNow]);
	
	pointZero = [NSDate date];
	transaction = [objectStore beginTransaction];
	@try {
		for (EXPerson* person in objects) {
			[transaction storeObject: person];
		}
		[transaction commit];
	} @catch (NSException* exception) {
		[transaction rollback];
	}
	NSLog(@"write test: %f secs", -[pointZero timeIntervalSinceNow]);
	
}

- (void)readTest1 {
	NSDate* pointZero = [NSDate date];
	EXTransaction* transaction = [objectStore beginTransaction];
	@try {
		//NSArray* objects = [transaction objectsOfClass: [EXPerson class]];
		NSArray* objects = [transaction proxiesOfClass: [EXPerson class]];
		for (EXPerson* person in objects) {
			person.UID;
		}
		NSLog(@"read test: %f secs", -[pointZero timeIntervalSinceNow]);
		NSLog(@"%u objects", [objects count]);
		[transaction rollback];
	} @catch (NSException* exception) {
		[transaction rollback];
	}

	/*pointZero = [NSDate date];
	objects = [objectStore objectsOfClass: [EXPerson class]];
	//objects = [objectStore proxiesOfClass: [EXPerson class]];
	NSLog(@"read test: %f secs", -[pointZero timeIntervalSinceNow]);
	NSLog(@"%u objects", [objects count]);
	//NSLog(@"%@", objects);*/
}

- (void)readTest2 {
	NSDate* pointZero = [NSDate date];
	NSArray* objects = [objectStore objectsOfClass: [EXPerson class]
											filter: EXQUERY(EXPerson* person, [person.firstName isEqual: @"First"])];
	NSLog(@"read test: %f secs", -[pointZero timeIntervalSinceNow]);
	NSLog(@"%u objects", [objects count]);
}

- (void)dealloc {
	[objectStore release];
	[super dealloc];
}

@end
